package top.z.template.parse;

import java.util.Arrays;

public enum TokenType {
    Dot("."),
    Hash("#"),
    Comma(","),
    Colon(":"),
    Backtick("`"),
    Semicolon(";"),
    singleQuote("'"),
    DoubleQuote("\""),
    QuestionMark("?"),

    LeftCurly("{"),
    RightCurly("}"),
    LeftBracket("["),
    RightBracket("]"),
    LeftParentheses("("),
    RightParentheses(")"),

    Plus("+"),
    Minus("-"),
    Divide("/"),
    Percent("%"),
    Multiply("*"),

    Or("||"),
    Not("!"),
    And("&&"),
    Xor("^"),

    EQ("=="),
    NQ("!="),
    LT("<"),
    LE("<="),
    GT(">"),
    GE(">="),

    Assignment("="),

    Text("text"),

    Null("Null"),
    Byte("Byte"),
    Long("Long"),
    Float("Float"),
    Short("Short"),
    Double("Double"),
    String("String"),
    Integer("Integer"),
    Boolean("Boolean"),
    Character("Character"),
    Identifier("Identifier");;

    private final        String      key;
    private static final TokenType[] valueList;

    TokenType(String key) {
        this.key = key;
    }

    public String key() {
        return key;
    }

    static {
        // 按文本长度对 Token 类型进行排序, 字符流使用此顺序首先匹配长度最长的标记
        valueList = TokenType.values();
        Arrays.sort(valueList, (o1, o2) -> {
            if (o1.key == null && o2.key == null) {
                return 0;
            }
            if (o1.key == null) {
                return 1;
            }
            if (o2.key == null) {
                return -1;
            }
            return o2.key.length() - o1.key.length();
        });
    }

    /**
     * 返回 Token 类型的数组, 根据其文本长度按降序排序, {@link CharacterStream} 使用它来匹配具有最长文本的标记类型, 例如: “<=”将在“<”之前匹配
     */
    public static TokenType[] valueList() {
        return valueList;
    }

}
